define(function(require,exports,module){
	require('validator');
	require('validator-lang');
	exports.init = function(){
		//ul选项卡的样式
		register_ul_li();
		register_form_action();
		register_form_action_();
		//用户名唯一性检测
		check_username_unique();
		check_email_unique();
		//点击注册按钮
		username_register();
		email_register();
	}	
	exports.operate = function(){

	}
	exports.win_resize = function(){

	}
	function register_ul_li(){
		$(".register_ul li ").live('click',function(){
			$(this).parent().find("li").removeClass("active-li");
			$(this).addClass('active-li');
			var show_div = $(this).attr('point-div');
			$(".li-point").hide();
			$("."+show_div).show();
		})
	}
	function check_username_unique(){
		$("input[name='register_name']").live('focusout',function(){
			var input_obj = $(this);
			if($(this).val() == ''){return;}
			var data = {
				'name':$(this).val()
			}
			$.ajax({
			   type: "POST",
			   url: register_var.ajax_check_name_unique_url,
			   data: data,
			   dataType:'json',
			   success: function(e){
					if(e.status){
						input_obj.popover('destroy');
						input_obj.popover({
							placement:'right',
							html:true,
							content:"<font style='color:green'>"+e.message+"</font>",
						}).popover('show');
					}else{
						input_obj.popover('destroy');
						input_obj.popover({
							placement:'right',
							html:true,
							content:"<font style='color:red'>"+e.message+"</font>",
						}).popover('show');
					}	   			
			   }
			});			
		})
	}
	function check_email_unique(){
		$("input[name='register_email']").live('focusout',function(){
			var input_obj = $(this);
			if($(this).val() == ''){return;}
			var is_true = true;
			$("input[name='register_email']").isValid(function(v){
				is_true &= v;
			});
			if(!is_true) {return;}
			var data = {
				'email':$(this).val()
			}
			$.ajax({
			   type: "POST",
			   url: register_var.ajax_check_email_unique_url,
			   data: data,
			   dataType:'json',
			   success: function(e){
					if(e.status){
						input_obj.popover('destroy');
						input_obj.popover({
							placement:'right',
							html:true,
							content:"<font style='color:green'>"+e.message+"</font>",
						}).popover('show');
					}else{
						input_obj.popover('destroy');
						input_obj.popover({
							placement:'right',
							html:true,
							content:"<font style='color:red'>"+e.message+"</font>",
						}).popover('show');
					}	   			
			   }
			});			
		})		
	}

	function register_form_action(){
		var register_form = $("form[name='register_form']");
		var check_box = $("form[name='register_form'] input[name='register_protocol']");
		var button_register = $("form[name='register_form'] button[name='button_register']");
		register_form.validator({
			stopOnError :false,//出错是否停止验证
			timely :true,	//是否实时验证   表单提交验证或是去焦点验证
			theme:'default',
			fields:{
				register_name:{
					rule:'required;username',
				},
				register_password:{
					rule:'密码:required;password',
				},
				register_repassword:{
					rule:'确认密码:required;match(register_password)',
				}
			}
		});

		check_box.live('click',function(){
			if($(this).attr('status') == 0){
				$(this).attr('status',1);
				button_register.removeAttr("disabled");
			}else{
				$(this).attr('status',0);
				button_register.attr("disabled",'disabled');
			}
			
		})
	}
	function register_form_action_(){
		var register_form_ = $("form[name='register_form_']");
		var check_box_ = $("form[name='register_form_'] input[name='register_protocol_']");
		var button_register_ = $("form[name='register_form_'] button[name='button_register_']");

		register_form_.validator({
			stopOnError :false,//出错是否停止验证
			timely :true,	//是否实时验证   表单提交验证或是去焦点验证
			theme:'default',
			fields:{
				register_email:{
					rule:'required;email',
				},
				register_password_:{
					rule:'密码:required;password',
				},
				register_repassword_:{
					rule:'确认密码:required;match(register_password_)',
				}
			}
		});

		check_box_.live('click',function(){
			if($(this).attr('status') == 0){
				$(this).attr('status',1);
				button_register_.removeAttr("disabled");
			}else{
				$(this).attr('status',0);
				button_register_.attr("disabled",'disabled');
			}
			
		})
	}

	function username_register(){
		$("button[name='button_register']").click(function(){
			//获取表单中验证的结果
			var is_true = true;
			$("#register_name").isValid(function(v1){
				is_true &= v1;
			});
			$("#register_password").isValid(function(v2){
				is_true &= v2;
			});
			$("#register_repassword").isValid(function(v3){
				is_true &= v3;
			});
			if(!is_true){
				$.Zebra_Dialog('继续完善注册？', {
				    'type':'error',
				    'buttons':  ['确定'],
				    'onClose':  function() {
				    }
				});	
				return;
			}
			
			var data = $("form[name='register_form']").serializeArray();
			var login_data = {
				username:data[0]['value'],
				userpwd:data[1]['value'],
				no_verify:1
			};
			$.ajax({
			   type: "POST",
			   url: register_var.ajax_register_by_username_url,
			   data: data,
			   dataType:'json',
			   success: function(e){
	   			if(e.status){
	   				$(".register_form").html('');
	   				var str = "<div class='register_ok'>恭喜您，已成功注册成为来吃会员</div><button class='btn btn-success register_login_direct'>直接登录</button>"
	   				$(".register_form").html(str);
	   				login_direct(login_data);
	   			}else{
					 $.Zebra_Dialog(e.message, {
					    'type':'error',
					    'title':'注册失败',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});	   				
	   			}
			   }
			});			
		});
	}

	function email_register(){
		$("button[name='button_register_']").click(function(){
			//获取表单中验证的结果
			var is_true = true;
			$("#register_email").isValid(function(v1){
				is_true &= v1;
			});
			$("#register_password_").isValid(function(v2){
				is_true &= v2;
			});
			$("#register_repassword_").isValid(function(v3){
				is_true &= v3;
			});
			if(!is_true){
				$.Zebra_Dialog('继续完善注册？', {
				    'type':'error',
				    'buttons':  ['确定'],
				    'onClose':  function() {
				    }
				});	
				return;
			}
			var data = $("form[name='register_form_']").serializeArray();
			var login_data = {
				username:data[0]['value'],
				userpwd:data[1]['value'],
				no_verify:1
			};
			$.ajax({
			   type: "POST",
			   url: register_var.ajax_register_by_email_url,
			   data: data,
			   dataType:'json',
			   success: function(e){
	   			if(e.status){
	   				$(".register_form_").html('');
	   				var str = "<div class='register_ok'>恭喜您，已成功注册成为来吃网会员</div><button class='btn btn-success register_login_direct'>直接登录</button>"
	   				$(".register_form_").html(str);
	   				login_direct(login_data);
	   			}else{
					 $.Zebra_Dialog(e.message, {
					    'type':'error',
					    'title':'注册失败',
					    'buttons':  ['确定'],
					    'onClose':  function() {
					    }
					});	   				
	   			}
			   }
			});			
		});
	}
	function login_direct(login_data){
		$(".register_login_direct").live('click',function(){
			$.ajax({
				 type: "POST",
				 url: register_var.login_url,
				 data: login_data,
				 dataType:'json',
				 success:function(e){
				 	if(e.status){
				 		window.location = register_var.index_url;
				 	}
				 }
			});			
		})
	}
});